<template>
    <div class="table">
        <el-breadcrumb separator="/" class="breadCrumb">
            <el-breadcrumb-item>{{$t('sidebar.controlAndAlert')}}</el-breadcrumb-item>
            <el-breadcrumb-item>{{$t('sidebar.taskAlertContact')}}</el-breadcrumb-item>
        </el-breadcrumb>
        <div class="crumbs">
            <el-row :gutter="15">
                <el-col :span="2" style="min-width:100px;">
                    <el-input
                        :placeholder="$t('taskAlertContact.pleaseInput',{ name: 'ID' })"
                        v-model="searchForm.id">
                    </el-input>
                </el-col>
                <el-col :span="4" style="line-height: 35px;">
                    <el-button size="small" type="primary" @click="searchData()">{{$t('taskAlertContact.search')}}</el-button>
                    <el-button size="small" type="primary" @click="refreshData()">{{$t('taskAlertContact.refresh')}}</el-button>
                </el-col>
                <el-col :span="1" class="floatRight">
                    <el-button size="small" type="primary" @click="addButton()">{{$t('taskAlertContact.add')}}</el-button>
                </el-col>
            </el-row>
        </div>
        <el-table :data="tableData" border style="width: 100%" :max-height="$store.state.allTable.tableHeight" v-loading="tableDataLoading"
                  :element-loading-text="$t('allTable.dataLoading')">
            <el-table-column prop="id" label="ID" width="60">
            </el-table-column>
            <el-table-column prop="contactName" :label="$t('taskAlertContact.contactName')" width="100">
            </el-table-column>
            <el-table-column prop="contactType" :label="$t('taskAlertContact.contactType')" width="100" :formatter="contactTypeShow">
            </el-table-column>
            <el-table-column prop="mobile" :label="$t('taskAlertContact.mobile')" min-width="100">
            </el-table-column>
            <el-table-column prop="email" :label="$t('taskAlertContact.email')" min-width="120">
            </el-table-column>
            <el-table-column prop="weixin" :label="$t('taskAlertContact.weixin')" min-width="100">
            </el-table-column>
            <el-table-column prop="qq" :label="$t('taskAlertContact.qq')" width="100">
            </el-table-column>
            <el-table-column prop="createDate" :label="$t('taskAlertContact.createDate')" width="150">
            </el-table-column>
            <el-table-column prop="modifyDate" :label="$t('taskAlertContact.modifyDate')" width="150">
            </el-table-column>
            <el-table-column prop="status" :label="$t('taskAlertContact.status')" width="60" :formatter="statusShow">
            </el-table-column>
            <el-table-column label="操作" :label="$t('taskAlertContact.operation')" width="100">
                <template scope="scope">
                    <el-button size="mini"
                               @click="handleEdit(scope)">{{$t('taskAlertContact.edit')}}</el-button>
                    <el-button size="mini" type="danger"
                               @click="deleteData(scope.row.id)">{{$t('taskAlertContact.delete')}}</el-button>
                </template>
            </el-table-column>
        </el-table>
        <div class="mgtb20" :class="{ pagination : tableData.length !== 0}">
            <el-pagination
                @size-change="handleSizeChange"
                @current-change ="handleCurrentChange"
                :page-sizes="$store.state.allTable.pageSizesChoice"
                :page-size="pageSizeGetter"
                layout="total, sizes, prev, pager, next, jumper"
                :total="sizeAll"
                :page-count="pageCount"
                v-if="tableData.length !== 0">
            </el-pagination>

        </div>
        <el-dialog :title="$t('sidebar.taskAlertContact')" size="mini" v-model="dialogFormVisible">
            <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" :inline="true" label-position="right">
                <el-row type="flex">
                    <el-col>
                        <el-form-item :label="$t('taskAlertContact.contactName')" prop="contactName">
                            <el-input v-model="ruleForm.contactName" :placeholder="$t('taskAlertContact.contactName')"></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col>
                        <el-form-item :label="$t('taskAlertContact.contactType')" prop="contactType">
                            <!--<el-input v-model="ruleForm.contactType" :placeholder="$t('taskAlertContact.contactType')"></el-input>-->
                            <el-select v-model="ruleForm.contactType" :placeholder="$t('taskAlertContact.contactType')" style="width: 130px">
                                <el-option
                                    key="0"
                                    label="自我游负责人"
                                    value="0">
                                </el-option>
                                <el-option
                                    key="1"
                                    label="自我游技术"
                                    value="1">
                                </el-option>
                                <el-option
                                    key="2"
                                    label="自我游商务"
                                    value="2">
                                </el-option>
                                <el-option
                                    key="3"
                                    label="自我游运营"
                                    value="3">
                                </el-option>
                                <el-option
                                    key="10"
                                    label="运营商"
                                    value="10">
                                </el-option>
                                <el-option
                                    key="11"
                                    label="运营商技术"
                                    value="11">
                                </el-option>
                                <el-option
                                    key="12"
                                    label="运营商商务"
                                    value="12">
                                </el-option>
                                <el-option
                                    key="13"
                                    label="运营商运营"
                                    value="13">
                                </el-option>
                                <el-option
                                    key="20"
                                    label="接口商"
                                    value="20">
                                </el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row type="flex">
                    <el-col>
                        <el-form-item :label="$t('taskAlertContact.mobile')" prop="mobile">
                            <el-input v-model="ruleForm.mobile" :placeholder="$t('taskAlertContact.mobile')"></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col>
                        <el-form-item :label="$t('taskAlertContact.email')" prop="email">
                            <el-input v-model="ruleForm.email" :placeholder="$t('taskAlertContact.email')"></el-input>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row type="flex">
                    <el-col>
                        <el-form-item :label="$t('taskAlertContact.weixin')" prop="weixin">
                            <el-input v-model="ruleForm.weixin" :placeholder="$t('taskAlertContact.weixin')"></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col>
                        <el-form-item :label="$t('taskAlertContact.qq')" prop="qq">
                            <el-input v-model="ruleForm.qq" :placeholder="$t('taskAlertContact.qq')"></el-input>
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="dialogFormVisible = false">{{$t('taskAlertContact.cancel')}}</el-button>
                <el-button type="primary" @click="addData('ruleForm')">{{$t('taskAlertContact.confirm')}}</el-button>
            </div>
        </el-dialog>
    </div>
</template>
<script>
    import { mapGetters,mapMutations } from 'vuex';
    import common from '../../assets/script/common';
    import { isEmail,isPhoneNum } from '../../assets/script/validate';
    export default {
        data() {
            const validateEmail = (rule, value, callback) => {
                if (!isEmail(value)) {
                    callback(new Error(this.$i18n.t('taskAlertContact.emailError')));
                } else {
                    callback();
                }
            };
            const validatePhoneNum = (rule, value, callback) => {
                if (!isPhoneNum(value)) {
                    callback(new Error(this.$i18n.t('taskAlertContact.telephoneError')));
                } else {
                    callback();
                }
            };
            return {
                tableData:[],
                pageName:'taskAlertContact',
                pageCount:1,
                sizeAll:0,
                dialogFormVisible: false,
                searchForm:{
                    id:''
                },
                ruleForm: {
                    id:'',
                    email: '',
                    mobile: '',
                    contactName: '',
                    qq:'',
                    contactType:'',
                    weixin:'',
                    createDate:'',
                    status:''
                },
                rules: {
                    email: [
                        { required: true, message: this.$i18n.t('taskAlertContact.pleaseInput',{ name: this.$i18n.t('taskAlertContact.email') }), trigger: 'blur' },
                        { required: true, trigger: 'blur' , validator: validateEmail}
                    ],
                    mobile: [
                        { required: true, message: this.$i18n.t('taskAlertContact.pleaseInput',{ name: this.$i18n.t('taskAlertContact.mobile') }), trigger: 'blur' },
                        { required: true, trigger: 'blur' , validator: validatePhoneNum}
                    ],
                    contactName: [
                        { required: true, message: this.$i18n.t('taskAlertContact.pleaseInput',{ name: this.$i18n.t('taskAlertContact.contactName') }), trigger: 'blur' }
                    ],
                    qq: [
                        { required: true, message: this.$i18n.t('taskAlertContact.pleaseInput',{ name: this.$i18n.t('taskAlertContact.qq') }), trigger: 'blur' },
                        { min: 5, max: 12, message: this.$i18n.t('taskAlertContact.maxLength'), trigger: 'blur' }
                    ],
                    contactType: [
                        { required: true, message: this.$i18n.t('taskAlertContact.pleaseInput',{ name: this.$i18n.t('taskAlertContact.contactType') }), trigger: 'change' }
                    ],
                    weixin: [
                        { required: true, message: this.$i18n.t('taskAlertContact.pleaseInput',{ name: this.$i18n.t('taskAlertContact.weixin') }), trigger: 'blur' },
                        { min: 5, max: 12, message: this.$i18n.t('taskAlertContact.maxLength'), trigger: 'blur' }
                    ]
                },
                hasTaskClass: true,
                dialogType:'',
                searchId:'',
                tableDataLoading:true
            }
        },
        created() {
            this.getData(1,this.$store.state.allTable.pageSize);
        },
        computed:{
            ...mapGetters([
                "pageSizeGetter",
            ]),
        },
        methods:{
            ...mapMutations([
                'pageSizeMutation'
            ]),
            handleSizeChange(val) {
                let self = this;
                self.pageSizeMutation(val);
                self.getData(1,self.$store.state.allTable.pageSize);
            },
            handleCurrentChange(val){
                common.changePageGoToTop(this,val);
            },
            getData(_pageSize,_pageNum) {
                let self = this;
                self.tableDataLoading = true;
                common.getTableList(this, 'taskAlertContact', _pageSize, _pageNum, self.searchForm, (data) => {
                    self.tableData = data.results ? data.results : [];
                    self.pageCount = data.pageCount;
                    self.sizeAll = data.sizeAll;
                    self.tableDataLoading = false;
                });
            },
            addData(formName) {
                let self = this;
                let __token = sessionStorage.getItem("token");
                let __ruleForm = this.ruleForm;

                this.$refs[formName].validate((valid) => {
                    if(valid){
                        if(this.dialogType === 'add'){
                            common.postTableList(self, 'taskAlertContact', __ruleForm, () => {
                                self.getData(1,self.$store.state.allTable.pageSize);
                                this.dialogFormVisible = false;
                                this.hasTaskClass = true;
                                for(let key in __ruleForm){
                                    __ruleForm[key] = '';
                                }
                                this.ruleForm = __ruleForm;
                            });
                        }else if(this.dialogType === 'edit'){
                            common.putTableList(self, 'taskAlertContact', __ruleForm, () => {
                                self.getData(1,self.$store.state.allTable.pageSize);
                                this.dialogFormVisible = false;
                                this.hasTaskClass = true;
                                for(let key in __ruleForm){
                                    __ruleForm[key] = '';
                                }
                                this.ruleForm = __ruleForm;
                            });
                        }

                    }
                });
            },
            deleteData(_id) {
                let self = this;
                common.deleteTableList(self, 'taskAlertContact', _id, () => {
                    self.getData(1,self.$store.state.allTable.pageSize);
                })
            },
            handleEdit(scopes) {
                let __ruleForm = this.ruleForm;
                this.hasTaskClass = false;
                for(let key in __ruleForm){
                    if(scopes.row[key] != null){
                        __ruleForm[key] = scopes.row[key].toString();
                    }else {
                        __ruleForm[key] = '';
                    }

                }
                this.ruleForm = __ruleForm;
                this.dialogFormVisible = true;
                this.dialogType = 'edit';
            },
            searchData() {
                let self = this;
                self.getData(1,self.$store.state.allTable.pageSize);
            },
            refreshData() {
                let self = this;
                for(let key in self.searchForm){
                    self.searchForm[key] = '';
                }
                self.getData(1,self.$store.state.allTable.pageSize);
            },
            addButton() {
                let __ruleForm = this.ruleForm;
                for(let key in __ruleForm){
                    __ruleForm[key] = '';
                }
                this.dialogFormVisible = true;
                this.dialogType = 'add';
                this.hasTaskClass = true;
            },
            statusShow(row, column) {
                if(row.status === 1)
                    return "正常";
            },
            contactTypeShow(row, column) {
                switch (row.contactType){
                    case 0:
                        return '自我游负责人';
                        break;
                    case 1:
                        return '自我游技术';
                        break;
                    case 2:
                        return '自我游商务';
                        break;
                    case 3:
                        return '自我游运营';
                        break;
                    case 10:
                        return '运营商';
                        break;
                    case 11:
                        return '运营商技术';
                        break;
                    case 12:
                        return '运营商商务';
                        break;
                    case 13:
                        return '运营商运营';
                        break;
                    case 20:
                        return '接口商';
                        break;
                }
            }
        }
    }
</script>
<style lang="stylus">

</style>
